package com.xiaojie.persistence.core.builder;

import com.xiaojie.persistence.core.session.DefaultSqlSessionFactory;
import org.dom4j.DocumentException;
import com.xiaojie.persistence.core.entity.Configuration;
import com.xiaojie.persistence.core.session.SqlSessionFactory;

import java.beans.PropertyVetoException;
import java.io.InputStream;

/**
 * @author 小杰
 * @date 2021/5/5
 * @description SqlSessionFactory构造者,用于构造SqlSessionFactory
 */
public class SqlSessionFactoryBuilder {

    public static SqlSessionFactory build(InputStream inputStream) throws DocumentException, PropertyVetoException, ClassNotFoundException {

        // 解析并构建配置文件
        XMLConfigBuilder xmlConfigBuilder = new XMLConfigBuilder();
        Configuration configuration = xmlConfigBuilder.parseConfig(inputStream);

        // 创建SqlSessionFactory,并设置Configuration
        SqlSessionFactory sqlSessionFactory = new DefaultSqlSessionFactory(configuration);

        return sqlSessionFactory;
    }

}
